package com.example.mapreduce.partition;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WordCountReduce
 * Package: com.example.mapreduce.wordcount
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-15
 * Time: 9:50
 */

/**
 * 都是hadoop自定义的类型
 * KEYIN reduce阶段输入的key的类型的 Text
 * VALUEIN reduce阶段输入的val类型 是IntWritable
 * KEYOUT reduce阶段输出的key类型 Text
 * VALUEOUT reduce阶段输出的val就是 IntWritable
 * map的输出是reduce的输入
 */
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
    private IntWritable outV = new IntWritable();
    //map阶段处理单词结果是 (abc,1)(abc,1)
    //reduce处理 (abc,(1,1)) abc是key (1,1)是val
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        int sum = 0;
        //累加
        //传进来的值是 abc,(1,1)
        for (IntWritable value : values){
            sum += value.get();
        }
        outV.set(sum);
        //写出
        context.write(key,outV);
    }
}
